package edu.gatech.cs2340.group7.wheresmystuffapp.views;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.ChangePasswordPresenter;

public class ChangePasswordView extends Activity implements
		View.OnClickListener {
	private String PACKAGE_NAME;
	private ChangePasswordPresenter myPresenter;
	private Button bCancel;
	private Button bChangePassword;
	private EditText etOldPassword;
	private EditText etNewPassword;
	private EditText etNewPasswordConfirm;
	private TextView tvChangeResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_password);
		initialize();

		myPresenter = new ChangePasswordPresenter(this);
		PACKAGE_NAME = getApplication().getPackageName();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bPasswordCancel:
			openParent(false);
			break;
		case R.id.bPasswordChange:
			String oldPassword = etOldPassword.getText().toString();
			String newPassword = etNewPassword.getText().toString();
			String newPasswordConfirm = etNewPasswordConfirm.getText().toString();
			myPresenter.changePassword(oldPassword, newPassword,
					newPasswordConfirm);
			break;
		}

	}

	private void openParent(boolean wait) {
		
		Intent intent = new Intent(this, AccountInformationView.class);
		intent.setPackage(PACKAGE_NAME);
		startActivity(intent);
	}

	/**
	 * Displays a confirmation message when the user attempts to change the password
	 * @param result the message that will be displayed
	 */
	public void setChangePasswordResult(String result) {
		tvChangeResult.setText(result);
		tvChangeResult.setTextColor(Color.RED);
		openParent(true);
	}

	/**
	 * Initializes all of the views in the XML layout, and sets the listeners for the buttons
	 */
	private void initialize() {
		bCancel = (Button) findViewById(R.id.bPasswordCancel);
		bChangePassword = (Button) findViewById(R.id.bPasswordChange);
		etOldPassword = (EditText) findViewById(R.id.etOldPassword);
		etNewPassword = (EditText) findViewById(R.id.etNewPassword);
		etNewPasswordConfirm = (EditText) findViewById(R.id.etConfirmNewPassword);
		tvChangeResult = (TextView) findViewById(R.id.tvChangePasswordResult);

		bCancel.setOnClickListener(this);
		bChangePassword.setOnClickListener(this);
	}

}
